package q384_shuffle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Solution_1 {
//    int[] originNums;
//    int[] shuffleNums;
//
//
//    public Solution_1(int[] nums) {
//        originNums = nums;
//        shuffleNums = nums;
//
//    }
//
//    public int[] reset() {
//        return originNums;
//    }
//
//    public int[] shuffle() {
//        List<Integer> shuffleList =  Arrays.stream(originNums).boxed().collect(Collectors.toList());
//        Random random = new Random();
//        for (int i = 0; i < shuffleNums.length; i++) {
//            int next_index = random.nextInt(shuffleList.size());
//            shuffleNums[i] = shuffleList.remove(next_index);
//        }
//
//        return shuffleNums;
//    }
//
//    public static void main(String[] args) {
//        int[] nums = {1,2,3};
//        Solution_1 s = new Solution_1(nums);
//        s.shuffle();
//        System.out.println(Arrays.toString(nums));
//    }

    /*
    考虑上方的实现方式
    如果我们初始时 将originNums和shuffle都指为nums的引用
    这会导致在我们reset时 原有的originNums也会发生更改 所以需要使用
    copy的方式来复制一个数组来使用
     */
    int[] nums;
    int[] original;

    public Solution_1(int[] nums) {
        this.nums = nums;
        this.original = new int[nums.length];
        System.arraycopy(nums, 0, original, 0, nums.length);
    }

    public int[] reset() {
        System.arraycopy(original, 0, nums, 0, nums.length);
        return nums;
    }

    public int[] shuffle() {
        int[] shuffled = new int[nums.length];
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < nums.length; ++i) {
            list.add(nums[i]);
        }
        Random random = new Random();
        for (int i = 0; i < nums.length; ++i) {
            int j = random.nextInt(list.size());
            shuffled[i] = list.remove(j);
        }
        System.arraycopy(shuffled, 0, nums, 0, nums.length);
        return nums;
    }

}
